package com.example.fastshare;

import java.util.ArrayList;
import java.util.List;

import com.example.fastshare.alljoyn.MainApp;
import com.example.fastshare.alljoyn.Utilities;

import android.content.Context;
import android.opengl.Visibility;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.QuickContactBadge;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.text.format.Time;

public class ListItemAdapter extends ArrayAdapter<Item> {
	private ArrayList<Item> _array;
	private int _resource;
	private Context _context;

	public ListItemAdapter(Context context, int textViewResourceId,
			ArrayList<Item> objects) {
		super(context, textViewResourceId, objects);
		this._array = objects;
		this._resource = textViewResourceId;
		this._context = context;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View historyView = convertView;

		final Item item = this._array.get(position);
		if (historyView == null) {
			if (item.isReceived) {
				historyView = new ListItem(this._context, true);
			} else
				historyView = new ListItem(this._context, false);
		}
		if (item != null) {
			QuickContactBadge avatar = ((ListItem) historyView).avatar;
			TextView content = ((ListItem) historyView).content;
			ImageView image = ((ListItem) historyView).image;
			TextView time = ((ListItem) historyView).time;

			if (item.getImageID() == null || item.getImageID().equals("")) {
				image.setVisibility(View.INVISIBLE);
			} else {
				image.setVisibility(View.VISIBLE);
				if (item.getImageID() != null) {
					image.setImageBitmap(Utilities.getImageLocal(item
							.getImageID()));
				} else {
					image.setImageResource(R.drawable.ic_launcher);
				}
			}
			if (item.getLastMsg() == null || item.getLastMsg().equals("")) {
				content.setVisibility(View.INVISIBLE);
			} else {
				content.setVisibility(View.VISIBLE);
				content.setText(item.getLastMsg());
			}
			if (item.getAvatarID().equals("1")) {
				avatar.setImageResource(R.drawable.a);
			}
			if (item.getAvatarID().equals("2")) {
				avatar.setImageResource(R.drawable.b);
			}
			if (item.getAvatarID().equals("3")) {
				avatar.setImageResource(R.drawable.c);
			}
			avatar.setFocusable(false);
			avatar.setClickable(false);
			avatar.setEnabled(false);
			if (item.getTime() == null) {
				time.setText(item.getStrTime());
			} else {
				time.setText(item.getTime().hour + ":" + item.getTime().minute
						+ ":" + item.getTime().second);
			}
		}
		return historyView;
	}

}
